package corp.edu.amadeus.utils.image;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import java.util.List;
import java.util.ArrayList;
import org.apache.log4j.Logger;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Properties;


public class ImageUtils {

	private static Logger log = Logger.getLogger(ImageUtils.class);

	public BufferedImage loadImage(String ref)
		throws Exception {
		BufferedImage bimg = null;
		bimg = ImageIO.read(new File(ref));
		return bimg;
	}

	public BufferedImage putFly(BufferedImage fondo, BufferedImage fly,
			int x, int y) {
		// Get the surfaces Graphics object
		Graphics2D g = (Graphics2D) fondo.createGraphics();
		g.drawImage(fly, null, x, y);
		return fondo;
	}

	public void putFly(BufferedImage fondo, BufferedImage fly,
			int x, int y, OutputStream os, String format)
			throws IOException {
		// Get the surfaces Graphics object
		Graphics2D g = (Graphics2D) fondo.createGraphics();
		g.drawImage(fly, null, x, y);
		ImageIO.write(fondo, format, os);
	}

	public BufferedImage putFly(String fondoPath, String flyPath,
			int x, int y)
			throws Exception {
		BufferedImage fondo = this.loadImage(fondoPath);
		BufferedImage mosca = this.loadImage(flyPath);
		// Get the surfaces Graphics object
		Graphics2D g = (Graphics2D) fondo.createGraphics();
		g.drawImage(mosca, null, x, y);
		return fondo;
	}

	public void putFly(String fondoPath, String flyPath,
			int x, int y, OutputStream os, String format)
			throws Exception {
		BufferedImage fondo = this.loadImage(fondoPath);
		BufferedImage mosca = this.loadImage(flyPath);
		// Get the surfaces Graphics object
		Graphics2D g = (Graphics2D) fondo.createGraphics();
		g.drawImage(mosca, null, x, y);
		ImageIO.write(fondo, format, os);
	}

	public void putText(BufferedImage fondo, String text,
			int x, int y, OutputStream os, String format)
			throws IOException {
		// Get the surfaces Graphics object
		Graphics2D g = (Graphics2D) fondo.createGraphics();
		g.drawString(text, x, y);
		ImageIO.write(fondo, format, os);
	}

	public BufferedImage putText(BufferedImage fondo, String text,
			int x, int y)
			throws IOException {
		// Get the surfaces Graphics object

		Graphics2D g = (Graphics2D) fondo.createGraphics();

		/*log.debug("---Font metrics(bytes): " + g.getFontMetrics().
				bytesWidth(text.getBytes(), 0, text.getBytes().length-1));
		log.debug("---Font metrics(chars): " + g.getFontMetrics().
				charsWidth(text.toCharArray(), 0, text.toCharArray().length-1));*/

		int textWidth =  g.getFontMetrics().
			charsWidth(text.toCharArray(), 0, text.toCharArray().length-1);
		log.debug("---Font metrics(chars): " + textWidth +
				" Text-length: " + text.length());
		log.debug("-- Fondo width: " + fondo.getWidth());

		if(textWidth > fondo.getWidth()) {
			List<String> comps = new ArrayList<String>();
			int it = (textWidth / (fondo.getWidth() - 10)) + 1;
			log.debug("It : " + it);
			int beginIndex=0, endIndex=0, realLength = text.length()/it ;
			for(int i=1; i<it ; i++) {
				endIndex = realLength * i;
				log.debug("BeginIndex: " + beginIndex +
						" EndIndex: " + endIndex);
				comps.add(text.substring(beginIndex, endIndex));
				beginIndex = endIndex;
			}
			comps.add(text.substring(beginIndex));
			int desplz = 0;
			for(String comp : comps) {
				g.drawString(comp, x, y + desplz);
				desplz+=13;
			}
		} else {
			g.drawString(text, x, y);
		}

		return fondo;
	}

	public void putText(String fondoPath, String text,
			int x, int y, OutputStream os, String format)
			throws Exception {
		BufferedImage fondo = this.loadImage(fondoPath);
		// Get the surfaces Graphics object
		Graphics2D g = (Graphics2D) fondo.createGraphics();
		g.drawString(text, x, y);
		ImageIO.write(fondo, format, os);
	}

	public void renderImage(BufferedImage image,
			OutputStream os, String format )
			throws IOException {
		ImageIO.write(image, format, os);
	}

	public Properties getImageProperties(String image_path)
		throws Exception {
		Properties props = new Properties();
		BufferedImage imagen = this.loadImage(image_path);
		System.out.println(imagen);
		// Get the surfaces Graphics object
		Graphics2D g = (Graphics2D) imagen.createGraphics();
		props.put("height", "" + imagen.getSampleModel().getHeight());
		props.put("width", "" + imagen.getWidth());
		props.put("minX", "" + imagen.getMinX());
		props.put("minY", "" + imagen.getMinY());
		System.out.println(props);
		return props;
	}

}